<? 
include("includes/common_public_inc.php");
$fp=fopen("admin/config/paylog.inc","a");
$str="\r\n\r\n";
$str.=date("d.m.Y H:i:s") . "\r\n";
foreach ($_REQUEST as $key => $value) {
	$str.="$key-$value\n";
}
fwrite($fp,$str);
include "payfunctions.php";

function markPaid($oid,$amount,$vendor)
{
	global $set_url,$pay_tbl;
	
	// Check that orderid and amount on order match
	if (!checkValidIdAndAmount("$oid","$amount"))
		return logPayInvalid($oid,$amount,$vendor . "_idamount");
		
	$sql = "select * from $pay_tbl where o_id = $oid";
	
	$res = q($sql);
	$row= mysql_fetch_array($res);
	$o_user = $row["o_user"];
	$o_package = $row["o_package"];
	
	// Make sure that user can�t push Refresh in his browser and get extra credits if paymet have been ok before
	$sql="select o_status from $pay_tbl where o_id=$oid";
	$res_check=q($sql);
	$row_check=mysql_fetch_array($res_check);
	$o_status=$row["o_status"];
	if ($o_status!='Paid')
	{
		// Update the Users table with the new data
		addPackage($o_user,$o_package,$oid);	
		// Complete order, mark it as paid
		completeOrder($oid,"Paid",$vendor,$amount,$o_package);
	}
	
	// Print out paid message
	return "<p><b>".LA_PAID."</b><br>".LA_PAID_MORE."<br /><br /><br /><a href='http://$set_url/member.php'><b><u>".LA_RETURN_ADMIN."</u></b></a></p>";
}

function markReversed($oid,$amount,$vendor)
{
	// Update database, set pur_paid status = 40
	global $pay_tbl,$set_url;
	$sql = "update $pay_tbl set o_status = 'Reversed' where o_id = $oid";
	$res = q($sql);
	return "<b>".LA_PAY_REVERSED_HEAD."</b><br>".LA_PAY_REVERSED_HEAD."<br><br /><br /><a href='http://$set_url/member.php'><b><u>".LA_RETURN_ADMIN."</u></b></a>";	
}

function markInvalid($oid,$amount,$vendor)
{
	// Update database, set pur_paid status = 20
	global $pay_tbl,$set_url;
	
	if (eregi("idamount",$vendor))
		$sql = "update $pay_tbl set o_status = 'Invalid amount/oid' where o_id = $oid";
	else 
		$sql = "update $pay_tbl set o_status = 'Invalid $amount-$vendor-$oid' where o_id = $oid";

	$res = q($sql);
	$lStatus="<b>INVALID: ". LA_PAY_INVALID_HEAD ."</b><br />". LA_PAY_INVALID_DESC ."<br /><br /><a href='http://$set_url/member.php'><b><u>".LA_RETURN_ADMIN."</u></b></a>";
	
	return $lStatus;
}

function payNeverHappen($oid,$amount)
{
	return "<b>Unknown error</b><br>Orderid: $oid<br>Amount: $amount<br>Vendor: $vendor";	
}

function markPending($oid,$amount,$vendor)
{
	global $pay_tbl,$set_url;
	// Update database, set pur_paid status = 30
	$sql = "update $pay_tbl set o_status = 'Pending' where o_id=$oid";
	$res = q($sql);
	return "<b>".LA_PAY_PENDING_HEAD."</b><br>".LA_PAY_PENDING_DESC . "<br /><br /><a href='http://$set_url/member.php'><b><u>".LA_RETURN_ADMIN."</u></b></a>";
	
}


function ValidIdAndAmount($oid,$amount)
{
	global $pay_tbl;
	$sql = "select o_amount from $pay_tbl where o_id = '$oid'";
	$res = q($sql);
	$row= mysql_fetch_array($res);
	$pur_amount = $row["o_amount"];
	$pur_amount = round($pur_amount);
	$amount = round($amount);
	
	if ($pur_amount==$amount)
		return true;
	else 
		return false;
}
echo "<html><head><title>$set_sitename</title></head><body>";
echo getReturnGateway();
echo "</body></html>";
?>